
package top.suven.base.http.processor.url;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import top.suven.base.http.processor.url.annotations.AnnotationListener;
import top.suven.base.http.processor.url.annotations.Observer;
import top.suven.base.http.processor.url.annotations.UrlRemote;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * Created by Alex on 2014/8/20
 */
@Component
public class UrlExplain implements AnnotationListener {
	
	private static Logger logger = LoggerFactory.getLogger(UrlExplain.class);

    private static Map<String, UrlRemote> map = new ConcurrentHashMap<>();

    private static final  int VERSION_OLD = 2500;



    /**
     * 只要实现AnnotationListener(一个空接口来的),
     * 这个方法就会被回调, url和cdn示例就会传进来啦
     * @param url url字符串
     * @param urlRemote url字符串上面的CDN注解实例
     */
    @Observer
    public void onVisit(String url, UrlRemote urlRemote) {
    	logger.info("init onVisit key value to UrlExplain Map<url,time> key=:" + url +"value =:" + urlRemote);
        map.put( url,urlRemote);
    }
    
   

    /**
     * 验证url 是否白名单中；UrlRemote 默认为白名称
     * @param url url字符串
     */
    public static boolean isWhite(String url) {
        UrlRemote urlex = map.get(url);
        if (urlex == null) {
            return false;
        }
        return urlex.isWhite();
    }

    public static boolean isWhiteVersion(Integer version){
        return null == version || version < VERSION_OLD;
    }

    /**
     * 验证url 是否白名单中；UrlRemote 默认为白名称
     * @param url url字符串
     */
    public static boolean isParamSign(String url) {
        UrlRemote urlex = map.get(url);
        if (urlex == null) {
            return false;
        }
        return urlex.isParamSign();
    }
    /**
     * url 必须登陆，强制验证
     * @param url
     * @return
     */
    public static boolean mustCheck(String url) {
        UrlRemote urlex = map.get(url);
        if (urlex == null) {
            return false;
        }
        return urlex.mustCheck();
    }
    
//    public static boolean isCdn(String url) {
//    	UrlRemote urlex = map.get(url);
//        if (urlex == null) {
//            return false;
//        }
//        return urlex.isCdn();
//    }
    
    public static String isValue(String url) {
    	UrlRemote urlex = map.get(url);
        if (urlex == null) {
            return "";
        }
        return urlex.value();
    }
   

}
